<?PHP
class DB
{
	private $db;
	public $customer;
	
	public function __construct()
	{
		$this->db = new SQLite3('mysqlitedb.db');
	}
	
	public function query($query = '')
	{
		$result = $this->db->query($query);
		
		return $result;
	}
	
	public function querySingle($query = '')
	{
		$result = $this->db->querySingle($query);
		
		return $result;
	}
}

class Record
{
	public function fill($assoc)
	{
		die ( __METHOD__ . " does not exists. Please, implement it firs");
	}
}

class Controller
{
	protected $sql;
	
	public function __construct($sql)
	{
		$this->sql = $sql;
	}
	public function table($filter = '')
	{
		$list = array();
		$sqlText = "SELECT * FROM {$this->tableName}";
		if ( !empty($filter) )
		{
			$sqlText .= " WHERE $filter";
		}
		$result = $this->sql->query($sqlText);
		while ( $assoc = $result->fetchArray() )
		{
			$item = $this->Create();
			$item->fill($assoc);
			
			$list[] = $item;
		}
		
		return $list;
	}
	public function byId($id)
	{
		$list = $this->table("id=$id");
		
		if ( count($list) > 0 )
		{
			return $list[0];
		}
		
		return $this->getEmpty();
	}
	public function delete($id)
	{
		$sqlText = "DELETE FROM {$this->tableName} WHERE id={$id}";
		
		$result = $this->sql->query($sqlText);
		
		return $result;
	}
	public function getEmpty()
	{
		return false;
	}
}

class English extends Record
{
	public $id;
	public $word;
	public $descr;
	
	public function fill($assoc)
	{
		$this->id = $assoc['id'];
		$this->word = $assoc['word'];
		$this->description = $assoc['description'];
	}
}

class Russian extends Record
{
	public function fill($assoc)
	{
		$this->id = $assoc['id'];
		$this->word = $assoc['word'];
		$this->description = $assoc['description'];
	}
}

class enruTranslation extends Record
{
	public function fill($assoc)
	{
		$this->id = $assoc['id'];
		$this->englishId = $assoc['englishId'];
		$this->russianId = $assoc['russianId'];
	}
}

class EnglishController extends Controller
{
	protected $tableName = 'English';
	
	public function Create()
	{
		return new English();
	}
	public function insert($item)
	{
		$sql = "INSERT INTO {$this->tableName} (word, description) VALUES ("
		. "'{$item->word}',"
		. "'{$item->description}'"
		. ")"
		;
		$res = $this->sql->query($sql);
		
		return $res;
	}
	public function update($item)
	{
		$sql = "UPDATE {$this->tableName} SET "
			. "word='{$item->word}', "
			. "description='{$item->description}' "
			. " WHERE id={$item->id}";

		$res = $this->sql->query($sql);
		
		return $res;
	}
}

class RussianController extends Controller
{
	protected $tableName = 'Russian';
	
	public function Create()
	{
		return new Russian();
	}
	
	public function insert($item)
	{
		$sql = "INSERT INTO {$this->tableName} (word, description) VALUES ("
		. "'{$item->word}',"
		. "'{$item->description}'"
		. ")"
		;
		$this->sql->query($sql);
	}
}

class enruTranslationController extends Controller
{
	protected $tableName = 'enrutr';
	
	public function Create()
	{
		return new enruTranslation();
	}
	
	public function insert($item)
	{
		$sql = "INSERT INTO {$this->tableName} (englishId, russianId) VALUES ("
		. "'{$item->englishId}',"
		. "'{$item->russianId}'"
		. ")"
		;
		$this->sql->query($sql);
	}
}

class dict
{
	public $enru;
	
	public function __construct($sql)
	{
		$this->English = new EnglishController($sql);
		$this->Russian = new RussianController($sql);
		$this->enru = new enruTranslationController($sql);
	}
	static function get()
	{
		global $dict;
		
		if ( empty($dict) )
		{
			$db = new DB();
			$dict = new dict($db);
		}
		return $dict;
	}
}
?>